<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/24
 * Time: 10:24
 */
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Model\PhotoModel;
use App\Model\ClassifyModel;
use App\Model\OnlieToolModel;
use App\Model\AddressModel;
use App\Model\OtherModel;
use Illuminate\Support\Facades\Crypt;

class WebController extends Controller
{
    public function __construct()
    {
        $data['photo'] = PhotoModel::orderBy('add_time', 'desc')->limit(8)->get();
        $data['classify'] = ClassifyModel::orderBy('add_time', 'desc')->get();
        $data['onlietool'] = OnlieToolModel::orderBy('add_time', 'desc')->get();
        $data['address'] = AddressModel::orderBy('add_time', 'desc')->get();
        $data['other'] = OtherModel::orderBy('add_time', 'desc')->get();
        session(['photo' => $data['photo']]);
        session(['classify' => $data['classify']]);
        session(['onlietool' => $data['onlietool']]);
        session(['address' => $data['address']]);
        session(['other' => $data['other']]);
    }

    function success($data = [], $message = '操作成功！')
    {
        return $this->jsonReturn($data, $message, 200);
    }

    function jsonReturn($data = [], $message = '', $code = 200)
    {
        $res = [
            "data" => $data,
            "message" => $message,
            "code" => $code,
        ];
        //add csrf-token
        $token = Crypt::encrypt(csrf_token());
        return response()->json($res)->header("token", $token);
    }
}